package edu.iyte.ceng316.ui.phonebook.mediator;

import edu.iyte.ceng316.model.Person;
import edu.iyte.ceng316.model.PhoneGroup;
import edu.iyte.ceng316.model.data.IPhoneBookModel;
import edu.iyte.ceng316.model.data.PhoneBookManager;
import edu.iyte.ceng316.model.data.PhoneBookSingleton;
import edu.iyte.ceng316.ui.phonebook.dialog.PhoneBookDialog;
import edu.iyte.ceng316.ui.phonebook.left.LeftPanel;
import edu.iyte.ceng316.ui.phonebook.right.PersonButtonPanel;
import edu.iyte.ceng316.ui.phonebook.right.PersonInfoPanel;

public class PhoneBookMediatorImpl implements IPhoneBookMediator {
    private LeftPanel leftPanel;
    private PersonInfoPanel personInfoPanel;
    
    IPhoneBookModel phoneBookManager;
    
    public PhoneBookMediatorImpl(){
    	phoneBookManager = PhoneBookSingleton.getModelManager();
    }
    
	public void setLeftPanel(LeftPanel leftPanel) {
		this.leftPanel = leftPanel;
	}

	public void setPersonInfoPanel(PersonInfoPanel personInfoPanel) {
		this.personInfoPanel = personInfoPanel;
	}

	public void createDepartment() {
		PhoneGroup group = new PhoneGroup("???");
		PhoneBookDialog dialog = new PhoneBookDialog(PhoneBookSingleton.getModelManager().getDepartmentNames(), group);
		dialog.setVisible(true);
		phoneBookManager.getGroups().addGroup(group);
		leftPanel.getDepartmentList().repaint();
		
	}
	
	public void editDepartment(){
		PhoneGroup group = phoneBookManager.getGroups().getGroups().get(leftPanel.getDepartmentList().getSelectedIndex());
		PhoneBookDialog dialog = new PhoneBookDialog(phoneBookManager.getDepartmentNames(), group);
		dialog.setVisible(true);
	}
	
	public void deleteDepartment(){
		phoneBookManager.getGroups().removeGroup(phoneBookManager.getGroups().getGroups().get(leftPanel.getDepartmentList().getSelectedIndex()));
		leftPanel.getDepartmentList().repaint();
	}
	
	public void createPerson() {
	  PhoneGroup group = PhoneBookSingleton.getModelManager().getGroups().getGroups().get(leftPanel.getDepartmentList().getSelectedIndex());
	  group.addPerson(new Person());
	  leftPanel.getDepartmentList().repaint();
	  personInfoPanel.getTable().repaint();
	}

	public void deletePerson() {
		PhoneGroup group = PhoneBookSingleton.getModelManager().getGroups().getGroups().get(leftPanel.getDepartmentList().getSelectedIndex());
		Person person = group.getPersons().get(personInfoPanel.getTable().getSelectedRow());
		group.removePerson(person);
		leftPanel.getDepartmentList().repaint();
		personInfoPanel.getTable().repaint();	
	}

}
